Imports System.Data.SqlClient
Imports System.Web.Security
Imports System.Data

Partial Class Registration
    Inherits System.Web.UI.Page

    ''' <summary>
    ''' Registers the user to the database
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub Submit1_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submit1.ServerClick

        Dim conn As SqlConnection
        Dim cmd As SqlCommand

        If Page.IsValid Then

            Try

                Dim connectionString As String = ConfigurationManager.ConnectionStrings("mydb").ConnectionString

                conn = New SqlConnection(connectionString)
                conn.Open()

                ' Create SqlCommand to insert values in the users table retrieving fields from the form.
                cmd = New SqlCommand("insert into users (uname, pwd, userRole, color, FK_groups) values ( @username , @passwd , 'User' , @color ,  @group  )", conn)
                cmd.Parameters.Add("@userName", SqlDbType.VarChar, 50)
                cmd.Parameters("@userName").Value = username.Text
                cmd.Parameters.Add("@passwd", SqlDbType.VarChar, 25)
                cmd.Parameters("@passwd").Value = passwd.Text
                cmd.Parameters.Add("@color", SqlDbType.VarChar, 25)
                cmd.Parameters("@color").Value = color.Text
                cmd.Parameters.Add("@group", SqlDbType.VarChar, 50)
                cmd.Parameters("@group").Value = groupList.SelectedItem.Text
                ' Execute command.
                cmd.ExecuteNonQuery()

                ' Cleanup command and connection objects.
                cmd.Dispose()
                conn.Dispose()
                Label1.Text = "Congratulations! You are now registered"
                Label1.Visible = True
                Panel1.Visible = False


            Catch ex As Exception
                ' Add error handling here for debugging.
                ' This error message should not be sent back to the caller.
                Label1.Text = "Warning! Registration failed!"
                Label1.Visible = True

                System.Diagnostics.Trace.WriteLine("[Registration] Exception " & ex.Message)
            End Try
        End If

    End Sub

    ''' <summary>
    ''' Adds to the dropdownlist the available groups to subscribe
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then

            Panel1.Visible = True

            Dim sqlConn As SqlConnection
            Dim sqlCmd As SqlCommand

            Dim sqlText As String = "select gname from groups"
            Dim myConn As String = ConfigurationManager.ConnectionStrings("mydb").ConnectionString

            Try
                sqlConn = New SqlConnection(myConn)
                sqlCmd = New SqlCommand(sqlText, sqlConn)
                sqlCmd.Connection.Open()

                groupList.DataSource = sqlCmd.ExecuteReader()
                groupList.DataBind()
                sqlConn.Close()
            Catch ex As Exception

            End Try
        End If
    End Sub

End Class
